/*****************************************************
*
*               COPYRIGHT (c) 2017-2019 Hargic Corporation
*                         All Rights Reserved
*
* The source code contained or described herein and all documents
* related to the source code ("Material") are owned by Hargic
* Corporation or its licensors.  Title to the Material remains
* with Hargic Corporation or its suppliers and licensors.
*
* The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used,
* copied, reproduced, modified, published, uploaded, posted, transmitted,
* distributed, or disclosed in any way except in accordance with the
* applicable license agreement.
*
* No license under any patent, copyright, trade secret or other
* intellectual property right is granted to or conferred upon you by
* disclosure or delivery of the Materials, either expressly, by
* implication, inducement, estoppel, except in accordance with the
* applicable license agreement.
*
* Unless otherwise agreed by Hargic in writing, you may not remove or
* alter this notice or any other notice embedded in Materials by Hargic
* or Hargic's suppliers or licensors in any way.
*
** Author: shengda.huang
** Date: 2019-04-07
*
*****************************************************/

/***** Include files ********************************/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <bcm2835.h>
#include "i2c_if.h"
#include "am23xx_i2c.h"

/***** Defines **************************************/

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/

/***** Define Prototype of functions ****************/

/**
 * @brief
 * @param  None
 * @retval None
 */
static int am23xx_init(void)
{
    return i2c_init(AM23XX_I2C_SLAVE_ADDR, AM23XX_I2C_RATE);
}

static int am23xx_i2c_read(unsigned char *pbuf, int len)
{
    return i2c_read(pbuf, len);
}

static int am23xx_i2c_write(unsigned char *pbuf, int len)
{
    return i2c_write(pbuf, len);
}

int main(int argc, char **argv)
{
    int ret = am23xx_init();
    printf("%s: am23xx_init ret %d\n", __func__, ret);
    if (ret != 0)
    {
        printf("%s: am23xx_init failed %d\n", __func__, ret);
        goto err;
    }

    am23xx_i2c_func_t func;
    unsigned short temp, humi;
    int cnt, success;

    func.i2c_write = am23xx_i2c_write;
    func.i2c_read  = am23xx_i2c_read;
    am23xx_register_i2c_func(&func);

    // printf("%s: wait %ds for sensor stable\n", __func__, 4);
    // sleep(4);

    cnt = success = 0;
    while (cnt < 10)
    {
        ret = am23xx_read_temp_humi(&temp, &humi);
        if (ret != 0)
        {
            printf("%s: am23xx_read_temp_humi failed %d\n", __func__, ret);
        }
        else
        {
            printf("%s: temp %d humi %d\n", __func__, temp, humi);
            success++;
        }

        cnt++;
        printf("%s: cnt %d success %d\n", __func__, cnt, success);
        sleep(2);
    }

err:
    i2c_deinit();
    return 0;
}

/* End of file */



